using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using WPFTemplateLib.WpfHelpers;

namespace WpfAnimationDemo.Controls.WaferRobotV3
{
	public partial class UcWaferRobotControl : UserControl
	{
		public UcWaferRobotControl()
		{
			InitializeComponent();
		}

		#region 依赖属性

		public static readonly DependencyProperty WaferStatusProperty = DependencyProperty.Register(nameof(WaferStatus), typeof(int), typeof(UcWaferRobotControl));
		/// <summary>
		/// 状态
		/// </summary>
		public int WaferStatus
		{
			get => (int)GetValue(WaferStatusProperty);
			set => SetValue(WaferStatusProperty, value);
		}

		public static readonly DependencyProperty RobotZActionProperty = DependencyProperty.Register(
			nameof(RobotZAction), typeof(WaferRobotZAction), typeof(UcWaferRobotControl),
			new PropertyMetadata(WaferRobotZAction.Z_Origin, RobotZActionPropertyChangedCallback));
		/// <summary>
		/// Z 轴动作
		/// </summary>
		public WaferRobotZAction RobotZAction
		{
			get => (WaferRobotZAction)GetValue(RobotZActionProperty);
			set => SetValue(RobotZActionProperty, value);
		}
		/// <summary>
		/// Z 轴动作变化
		/// </summary>
		private static void RobotZActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var control = d as UcWaferRobotControl;
			if(control == null)
				return;

			var oldAct = (WaferRobotZAction)e.OldValue;
			var newAct = (WaferRobotZAction)e.NewValue;
			if(newAct != oldAct)
			{
				VisualStateManager.GoToState(control, newAct.ToString(), true);
			}
		}

		public static readonly DependencyProperty RobotXActionProperty = DependencyProperty.Register(
			 nameof(RobotXAction), typeof(WaferRobotXAction), typeof(UcWaferRobotControl),
			 new PropertyMetadata(WaferRobotXAction.X_Origin, RobotXActionPropertyChangedCallback));
		/// <summary>
		/// X 轴动作
		/// </summary>
		public WaferRobotXAction RobotXAction
		{
			get => (WaferRobotXAction)GetValue(RobotXActionProperty);
			set => SetValue(RobotXActionProperty, value);
		}
		/// <summary>
		/// X 轴动作变化
		/// </summary>
		private static void RobotXActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var control = d as UcWaferRobotControl;
			if(control == null)
				return;

			var oldAct = (WaferRobotXAction)e.OldValue;
			var newAct = (WaferRobotXAction)e.NewValue;
			if(newAct != oldAct)
			{
				VisualStateManager.GoToState(control, newAct.ToString(), true);
			}
		}

		public static readonly DependencyProperty RobotTActionProperty = DependencyProperty.Register(
			 nameof(RobotTAction), typeof(WaferRobotTAction), typeof(UcWaferRobotControl),
			 new PropertyMetadata(WaferRobotTAction.T_Origin, RobotTActionPropertyChangedCallback));
		/// <summary>
		/// T 轴动作
		/// </summary>
		public WaferRobotTAction RobotTAction
		{
			get => (WaferRobotTAction)GetValue(RobotTActionProperty);
			set => SetValue(RobotTActionProperty, value);
		}
		/// <summary>
		/// T 轴动作变化
		/// </summary>
		private static void RobotTActionPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var control = d as UcWaferRobotControl;
			if(control == null)
				return;

			var oldAct = (WaferRobotTAction)e.OldValue;
			var newAct = (WaferRobotTAction)e.NewValue;
			//if (newAct != oldAct)
			{
				VisualStateManager.GoToState(control, newAct.ToString(), true);
			}
		}

		#region [依赖属性] 运行动作
		/// <summary>
		/// 运行动作
		/// </summary>
		public WaferRobotRunAction RobotRunAction
		{
			get => (WaferRobotRunAction)GetValue(RobotRunActionProperty);
			set => SetValue(RobotRunActionProperty, value);
		}
		/// <summary>
		/// [依赖属性] 运行动作
		/// </summary>
		public static readonly DependencyProperty RobotRunActionProperty =
			 DependencyProperty.Register(nameof(RobotRunAction), typeof(WaferRobotRunAction), typeof(UcWaferRobotControl),
				new PropertyMetadata(WaferRobotRunAction.Run_Origin, RobotRunActionChangedCallback));
		/// <summary>
		/// 运行动作 变动处理方法
		/// </summary>
		private static void RobotRunActionChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var control = d as UcWaferRobotControl;
			if(control == null)
				return;

			var oldAct = (WaferRobotRunAction)e.OldValue;
			var newAct = (WaferRobotRunAction)e.NewValue;
			if(newAct != oldAct)
			{
				VisualStateManager.GoToState(control, newAct.ToString(), true);
			}
		}
		#endregion

		#endregion

		#region 命令

		#region [命令] 旋转机械手
		private ICommand _RotateRobotCmd;
		/// <summary>
		/// [命令] 旋转机械手
		/// </summary>
		public ICommand RotateRobotCmd => _RotateRobotCmd ??= new RelayCommand<string>(ExecuteRotateRobotCmd);
		private void ExecuteRotateRobotCmd(string para)
		{
			BeginTheStory(ResourceKeys.KEY_STORY_ROBOT_ROTATE);
		}
		#endregion

		#region [命令] 旋转大臂
		private ICommand _RotateBigArmCmd;
		public ICommand RotateBigArmCmd => _RotateBigArmCmd ??= new RelayCommand<string>(ExecuteRotateBigArmCmd);
		private void ExecuteRotateBigArmCmd(string para)
		{
			BeginTheStory(ResourceKeys.KEY_STORY_BIGARM_ROTATE);
		}
		#endregion

		#region [命令] 上移大臂（使用执行动画的方式）
		private ICommand _UpBigArmUseAnimationCmd;
		public ICommand UpBigArmUseAnimationCmd => _UpBigArmUseAnimationCmd ??= new RelayCommand<string>(ExecuteUpBigArmCmd);
		private void ExecuteUpBigArmCmd(string para)
		{
			BeginTheStory(ResourceKeys.KEY_STORY_BIGARM_UP);
		}
		#endregion

		#region [命令] 上移大臂（使用修改 Canvas 的 Top 的方式）
		private ICommand _UpBigArmUseCanvasTopCmd;
		public ICommand UpBigArmUseCanvasTopCmd => _UpBigArmUseCanvasTopCmd ??= new RelayCommand<string>(ExecuteUpBigArmUseCanvasTopCmd);
		private void ExecuteUpBigArmUseCanvasTopCmd(string para)
		{
			var top = Canvas.GetTop(bigArm);
			Canvas.SetTop(bigArm, top - 10);
		}
		#endregion

		#endregion

		/// <inheritdoc />
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			VisualStateManager.GoToState(this, WaferRobotZAction.Z_Origin.ToString(), true);
			VisualStateManager.GoToState(this, WaferRobotXAction.X_Back.ToString(), true);
			VisualStateManager.GoToState(this, WaferRobotTAction.T_Origin.ToString(), true);
			VisualStateManager.GoToState(this, WaferRobotRunAction.Run_Origin.ToString(), true);
		}

		/// <summary>
		/// 开始指定的动画
		/// </summary>
		private void BeginTheStory(string resourceKey)
		{
			Storyboard story = Resources[resourceKey] as Storyboard;
			if(story == null)
			{
				return;
			}

			story.Begin();
		}
	}
}
